package com.ruoyi.business.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.business.domain.MessageCenter;
import com.ruoyi.business.domain.vo.CommonReviewVO;
import com.ruoyi.business.service.IMessageCenterService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.WxCar;
import com.ruoyi.business.service.IWxCarService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆认证管理Controller
 * 
 * @author asher
 * @date 2021-11-19
 */
@RestController
@RequestMapping("/business/wx_car")
public class WxCarController extends BaseController
{
    @Autowired
    private IWxCarService wxCarService;

    @Autowired
    private IMessageCenterService messageCenterService;

    /**
     * 查询车辆认证管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:wx_car:list')")
    @GetMapping("/list")
    public TableDataInfo list(WxCar wxCar)
    {
        startPage();
        List<WxCar> list = wxCarService.selectWxCarList(wxCar);
        return getDataTable(list);
    }

    /**
     * 导出车辆认证管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:wx_car:export')")
    @Log(title = "车辆认证管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WxCar wxCar)
    {
        List<WxCar> list = wxCarService.selectWxCarList(wxCar);
        ExcelUtil<WxCar> util = new ExcelUtil<WxCar>(WxCar.class);
        return util.exportExcel(list, "车辆认证管理数据");
    }

    /**
     * 获取车辆认证管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:wx_car:query')")
    @GetMapping(value = "/{carId}")
    public AjaxResult getInfo(@PathVariable("carId") Long carId)
    {
        return AjaxResult.success(wxCarService.selectWxCarByCarId(carId));
    }

    /**
     * 新增车辆认证管理
     */
    @PreAuthorize("@ss.hasPermi('business:wx_car:add')")
    @Log(title = "车辆认证管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxCar wxCar)
    {
        return toAjax(wxCarService.insertWxCar(wxCar));
    }

    /**
     * 修改车辆认证管理
     */
    @PreAuthorize("@ss.hasPermi('business:wx_car:edit')")
    @Log(title = "车辆认证管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxCar wxCar)
    {
        return toAjax(wxCarService.updateWxCar(wxCar));
    }

    /**
     * 删除车辆认证管理
     */
    @PreAuthorize("@ss.hasPermi('business:wx_car:remove')")
    @Log(title = "车辆认证管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{carIds}")
    public AjaxResult remove(@PathVariable Long[] carIds)
    {
        return toAjax(wxCarService.deleteWxCarByCarIds(carIds));
    }

    /**
     * 审核人脸识别
     */
    @Log(title = "审核人脸识别", businessType = BusinessType.INSERT)
    @PostMapping("/review")
    public AjaxResult review(@RequestBody CommonReviewVO commonReviewVO) throws IOException {
        int i = wxCarService.reviewCar(commonReviewVO);
        if (i == 0) {
            return AjaxResult.error("车牌审核失败，请检查该用户小区是否绑定了人脸识别设备");
        }
        MessageCenter messageCenter = new MessageCenter();
        messageCenter.setTitle("车牌审核结果");
        String content = "车牌审核通过,感谢您的使用";
        if (commonReviewVO.getStatus().equals("0")) {
            content = "审核被驳回,驳回理由：" + commonReviewVO.getContent();
        }
        messageCenter.setContent("[车牌识别]" + content);
        messageCenter.setIsRead("0");
        messageCenter.setModule("车牌识别");
        messageCenter.setDestUserId(commonReviewVO.getUserId().toString());
        messageCenterService.insertMessageCenter(messageCenter);
        return AjaxResult.success();
    }
}
